<template>
  <div class="footer_circle">
    <div class="header">
      <table class="header_tab01">
        <tr>
          <td style="font-size: xx-large; color: black">动态</td>
          <td>直播</td>
          <td>聊天室</td>
          <td>实盘</td>
          <td>公告</td>
          <td>快讯</td>
          <td style="text-align: right">
            <img :src="require('@/assets/newsicon/search.png')" />
          </td>
          <td style="text-align: right">
            <img :src="require('@/assets/newsicon/message.png')" />
          </td>
        </tr>
      </table>
    </div>
    <div class="mid">
      <div class="mid_hd">
       
        <p class="p-container">
			<p class="p03">
			  <img src="../../assets/newsicon/direct_red.png" />
			  <span>广场</span>
			</p>  
          <img class="icon hot-icon" :src="require('@/assets/newsicon/hot.png')" />
          <img class="icon mark-icon" :src="require('@/assets/newsicon/mark.png')" />
          <img class="icon comment-icon" :src="require('@/assets/newsicon/comment.png')" />
        </p>
      </div>
	  
      <div class="mid_list" v-for="(data, i) in datas" :key="i">
        <table>
          <tr id="t1">
            <td rowspan="2" width="12%">
              <p><img width="70px" height="70px" :src="data.userPhoto" /></p>
            </td>
            <td width="76%">
              {{ data.username }}
              <div id="t1_span">vip{{ data.vipLevel }}</div>
            </td>
            <td rowspan="2" style="text-align: right">
              <img width="20px" height="20px" :src="require('@/assets/newsicon/more.png')" />
            </td>
          </tr>
          <tr id="t2">
            <td>
              <div>{{ data.time }}分钟前</div>
            </td>
          </tr>
        </table>
        <div class="mid_list_text">
          <p id="p1">{{ data.comment }}</p>
          <p id="p2">查看翻译</p>
        </div>
        <div class="picture" v-if="data.commentImg.length > 0">
          <div class="grid-container">
            <div 
              class="grid-item" 
              v-for="(img, index) in data.commentImg.slice(0, 9)" 
              :key="index"
            >
              <img :src="img" alt="图片" />
            </div>
          </div>
        </div>
        <div class="ft">
          <table>
            <tr>
              <td class="ft_t3">
                 <el-button icon="Star" circle :class="{'liked': isLiked}" @click="toggleLike"/>
                <span>赞赏</span>
              </td>
              <td class="ft_t1">
				<button class="share-btn" @click="shareContent">
					<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-share" viewBox="0 0 16 16">
					  <path d="M13.5 1a1.5 1.5 0 1 0 0 3 1.5 1.5 0 0 0 0-3zM11 2.5a2.5 2.5 0 1 1 .603 1.628l-6.718 3.12a2.499 2.499 0 0 1 0 1.504l6.718 3.12a2.5 2.5 0 1 1-.488.876l-6.718-3.12a2.5 2.5 0 1 1 0-3.256l6.718-3.12A2.5 2.5 0 0 1 11 2.5zm-8.5 4a1.5 1.5 0 1 0 0 3 1.5 1.5 0 0 0 0-3zm11 5.5a1.5 1.5 0 1 0 0 3 1.5 1.5 0 0 0 0-3z"/>
					</svg>
					<span>分享</span>
				</button>  
              </td>
              <td class="ft_t3">
                <el-button @click="toggleCommentInput" :icon="Edit" circle />
                <span>评论</span>
			    <!-- 评论输入框 -->
			    <div v-if="showCommentInput" class="comment-box">
			      <el-input
			        v-model="commentText"
			        placeholder="请输入评论内容"
			        type="textarea"
			        rows="3"
			      />
			      <div class="comment-actions">
			        <el-button type="primary" @click="submitComment">发表评论</el-button>
			        <el-button @click="cancelComment">取消</el-button>
			      </div>
			    </div>	
              </td>
              <td class="ft_t4">
                <button ref="likeButton" class="like-btn" @click="handleLikeClick">
                    <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-hand-thumbs-up" viewBox="0 0 16 16">
                      <path d="M8.864.046C7.908-.193 7.02.53 6.956 1.466c-.072 1.051-.23 2.016-.428 2.59-.125.36-.479 1.013-1.04 1.639-.557.623-1.282 1.178-2.131 1.41C2.685 7.288 2 7.87 2 8.72v4.001c0 .845.682 1.464 1.448 1.545 1.07.114 1.564.415 2.068.723l.048.03c.272.165.578.348.97.484.397.136.861.217 1.466.217h3.5c.937 0 1.599-.477 1.934-1.064a1.86 1.86 0 0 0 .254-.912c0-.152-.023-.312-.077-.464.201-.263.38-.578.488-.901.11-.33.172-.762.004-1.149.069-.13.12-.269.159-.403.077-.27.113-.568.113-.857 0-.288-.036-.585-.113-.856a2.144 2.144 0 0 0-.138-.362 1.9 1.9 0 0 0 .234-1.734c-.206-.592-.682-1.1-1.2-1.272-.847-.282-1.803-.276-2.516-.211a9.84 9.84 0 0 0-.443.05 9.365 9.365 0 0 0-.062-4.509A1.38 1.38 0 0 0 9.125.111L8.864.046zM11.5 14.721H8c-.51 0-.863-.069-1.14-.164-.281-.097-.506-.228-.776-.393l-.04-.024c-.555-.339-1.198-.731-2.49-.868-.333-.036-.554-.29-.554-.55V8.72c0-.254.226-.543.62-.65 1.095-.3 1.977-.996 2.614-1.708.635-.71 1.064-1.475 1.238-1.978.243-.7.407-1.768.482-2.85.025-.362.36-.594.667-.518l.262.066c.16.04.258.143.288.255a8.34 8.34 0 0 1-.145 4.725.5.5 0 0 0 .595.644l.003-.001.014-.003.058-.014a8.908 8.908 0 0 1 1.036-.157c.663-.06 1.457-.054 2.11.164.175.058.45.3.57.65.107.308.087.67-.266 1.022l-.353.353.353.354c.043.043.105.141.154.315.048.167.075.37.075.581 0 .212-.027.414-.075.582-.05.174-.111.272-.154.315l-.353.353.353.354c.047.047.109.177.005.488a2.224 2.224 0 0 1-.505.805l-.353.353.353.354c.006.005.041.05.041.17a.866.866 0 0 1-.121.416c-.165.288-.503.56-1.066.56z"/>
                    </svg>
                    <span>点赞</span>
                    <span id="likeFeedback" class="feedback-text"></span>
                    <span id="likeCount" class="like-count">{{ likeCount }}</span>
                  </button>
              </td>
            </tr>
          </table>
        </div>
      </div>
    </div>
    <div class="add">
      <p>
        <router-link to="/addNew">
          <svg xmlns="http://www.w3.org/2000/svg" style="width: 50px;height: 50px;color:white" width="16" height="16" fill="currentColor" class="bi bi-plus-circle" viewBox="0 0 16 16">
            <path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"/>
            <path d="M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4z"/>
          </svg>
        </router-link>
      </p>
    </div>
  </div>
</template>



<script setup>
import { ref, onMounted } from "vue";
import newsData from "@/assets/News.json"; // 引入 JSON 文件
import {Edit,} from '@element-plus/icons-vue'
import { ElMessage } from "element-plus";

const datas = ref([]);

// 在 Vue 中处理图片路径的函数
const getImgPath = (path) => {
  try {
    // 使用 require 动态加载图片
    return require(`@/assets/${path}`);
  } catch (error) {
    console.error("图片加载失败：", path, error);
    return "";
  }
};

onMounted(() => {
  // 处理 JSON 中的图片路径
  datas.value = newsData.data.news.map(item => {
    return {
      ...item,
      userPhoto: getImgPath(item.userPhoto),  // 使用 getImgPath 来处理图片路径
      commentImg: item.commentImg.map(img => getImgPath(img))  // 对 commentImg 中的每个路径使用 getImgPath
    };
  });
});

const likeButton = ref(null);
let count = ref(0);
const likeFeedback = ref("");
const likeCount = ref(0);

// 按钮点击事件
const handleLikeClick = () => {
  count.value++;
  likeCount.value = count.value; // 更新点赞数
  likeFeedback.value = "感谢您的点赞！"; // 显示反馈
  setTimeout(() => {
    likeFeedback.value = ""; // 隐藏反馈
  }, 2000); // 2秒后隐藏
};

// 设置事件监听器
onMounted(() => {
  if (likeButton.value) {
    likeButton.value.addEventListener("mouseover", () => {
      likeButton.value.style.color = "red";
      likeButton.value.style.transform = "scale(1.1)";
    });

    likeButton.value.addEventListener("mouseout", () => {
      likeButton.value.style.color = "";
      likeButton.value.style.transform = "scale(1)";
    });

    likeButton.value.addEventListener("click", handleLikeClick);
  }
});

const showCommentInput = ref(false); // 控制输入框显示/隐藏
const commentText = ref(""); // 绑定用户输入的评论内容

// 点击按钮显示或隐藏输入框
const toggleCommentInput = () => {
  showCommentInput.value = !showCommentInput.value;
};

// 提交评论功能
const submitComment = () => {
  if (commentText.value.trim() === "") {
    ElMessage({
      message: "评论内容不能为空！",
      type: "error",
      duration: 2000,
    	offset: 50
    });
	return;
  }
  console.log("用户评论：", commentText.value); // 这里可以将评论发送到后台
  ElMessage({
    message: "评论成功！",
    type: "success",
    duration: 2000,
  	offset: 50
  });
  commentText.value = ""; // 清空输入框
  showCommentInput.value = false; // 隐藏输入框
};

// 取消评论
const cancelComment = () => {
  commentText.value = ""; // 清空输入框
  showCommentInput.value = false; 
  ElMessage({
    message: "取消评论！",
    type: "info",
    duration: 2000,
  	offset: 50
  });// 隐藏输入框
};

const isLiked = ref(false);

// 切换赞赏状态
const toggleLike = () => {
  isLiked.value = !isLiked.value; // 切换状态
};

// 分享功能逻辑
const shareContent = () => {
  // 模拟分享逻辑
  ElMessage({
    message: "已触发分享功能！",
    type: "success",
    duration: 2000,
	offset: 50
  });
};
</script>



<style scoped>
.footer_circle {
  width: 98%; /* 设置宽度为 98% */
  max-width: 1260px; /* 最大宽度，避免过于宽松 */
  margin: 0 auto; /* 居中显示 */
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.header_tab01 {
  width: 100%;
  margin-top:4%
}

.header_tab01 td {
  font-size: 2rem; /* 调整字体大小 */
  color: #979797;
  padding: 8px;
}

.header .header_tab01 img {
  width: 24px; /* 图标大小调整 */
  height: 24px;
}
/* 设置外部容器样式 */
.p-container {
  display: flex; /* 使用 flex 布局 */
  justify-content: space-between; /* 两端对齐，保持各图片间的间距 */
  align-items: center; /* 垂直居中对齐 */
  width: 98%; /* 大容器宽度 */
  margin: 0; /* 移除默认的 p 标签边距 */
  padding: 0; /* 移除默认的 p 标签内边距 */
  height: 45px;
}

/* 图片公共样式 */
.p-container .icon {
  display: inline-block; /* 保持图片为行内块 */
  object-fit: cover; /* 确保图片不会变形 */
}

/* 各图片单独样式 */
.p-container .hot-icon {
  width: 4%; /* 调整图片宽度 */
  margin-left: -10%;
}

.p-container .mark-icon {
  width: 4%;
}

.p-container .comment-icon {
  width: 4%;
  border-radius: 50%; /* 圆形图片 */
  margin-right: 5%;
}

/** Middle */
.mid {
  padding: 10px;
  margin-top: -25px;
}

.mid_hd {
  margin-top: 5px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.mid_hd img {
  width: 40px;
  height: 40px;
  margin-top: 3px;
}

.mid_hd p {
  background-color: #f2f2f2;
  text-align: center;
  padding: 8px;
  border-radius: 10px;
  overflow: hidden;
  margin-top: 2%;
}

.mid_hd .p03 {
  background-color: #ffebec;
  color: #e53c3d;
}

.mid_list {
  width: 100%;
  box-shadow: 0px 10px 0px 0px #f6f6f6;
  padding: 10px;
  margin-top: 10px;
}

.mid_list table {
  width: 100%;
}

.mid_list #t1 p {
  background-color: #65b2a2;
  width: 75px;
  height: 75px;
  border-radius: 100%;
  overflow: hidden;
  text-align: center;
  margin-left: 8px;
}

/** vip等级设置 */
#t1_span {
  margin-left: 5px;
  width: 30px;
  height: 18px;
  border-radius: 5px;
  background-color: #58a4f2;
  display: inline-block;
  font-size: small;
  color: #f9fafc;
  text-align: center;
  vertical-align: middle;
  padding-top: 2px;
}

.mid_list #t1 span {
  vertical-align: middle;
  color: #f9fafc;
  font-size: xx-small;
}

.mid_list #t2 {
  font-size: 0.8rem; /* 调整字体大小 */
  color: #979797;
}

.mid_list_text {
  margin-top: -10px;
}

.mid_list_text #p1 {
  font-size: 1rem;
  line-height: 1.5;
}

.mid_list_text #p2 {
  color: #979797;
  font-size: 0.75rem;
  margin-top: -10px;
  margin-left: 10px;
}



.bi {
  transform: scale(1.5); /* 将图标大小放大两倍 */
}

/* 默认按钮样式 */
.like-btn {
  background-color: white;
  border: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  padding: 5px 10px;
  font-size: 16px;
  transition: all 0.3s ease;
}

/* 鼠标悬停时效果 */
.like-btn:hover {
  color: red;
  transform: scale(1.1); /* 放大 */
}

/* 按钮点击时效果 */
.like-btn:active {
  color: red;
  transform: scale(1.1); /* 放大 */
}

/* 点赞总数的样式 */
.like-count {
  margin-left: 8px;
  font-weight: bold;
  font-size: 14px;
}

/* 点赞反馈文本 */
.feedback-text {
  margin-left: 5px;
  font-size: 14px;
  color: green;
}




.picture {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
}

.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); /* 自动调整列数 */
  gap: 5px; /* 设置图片之间的间距 */
  width: 100%;
  max-width: 600px; /* 最大宽度 */
  margin: 0 auto;
}

.grid-item {
  width: 100%;
  aspect-ratio: 1; /* 保持图片为正方形 */
  overflow: hidden;
  cursor: pointer; 
}

.grid-item img {
  width: 100%;
  height: 100%;
  object-fit: cover; /* 确保图片填充并裁剪 */
  border-radius: 4px; /* 可选：让图片圆角 */
  transition: transform 0.3s ease;
}
.grid-item img:hover {
  transform: scale(1.05); /* 鼠标悬浮时略微放大 */
}


.ft {
  margin-top: 8px;
}

.ft table {
  width: 100%;
  text-align: center;
}

.ft img {
  width: 24px;
  height: 24px;
  vertical-align: middle;
}

.ft_t1 {
  color: #979797;
  font-size: 0.9rem;
}
.ft_t3 {
  color: #979797;
  font-size: 0.75rem;
  transform: scale(1.21);
}
/* 透明背景的按钮样式 */
.share-btn {
  background-color: transparent; /* 设置背景透明 */
  border: none; /* 去掉边框 */
  color: currentColor; /* 使用当前文本颜色 */
  cursor: pointer; /* 鼠标悬停时显示手形指针 */
  padding: 8px;
  transition: color 0.3s ease; /* 添加悬停过渡效果 */
}

.share-btn:hover {
  color: #007bff; /* 鼠标悬停时改变文字和图标颜色 */
}

.liked {
  background-color: #ffd700 !important; /* 设置背景为金黄色 */
  color: #fff !important; /* 字体颜色为白色 */
  border-color: #ffd700 !important; /* 边框颜色与背景一致 */
}

.ft span {
  margin-left: 5px;
  vertical-align: middle;
}

/** 添加按钮 */
.add {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 10;
}

.add img {
  width: 50px;
  height: 50px;
}

.add p {
  width: 70px;
  height: 70px;
  background-color: indianred;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 150%;
}

/* 响应式设计 */
@media screen and (max-width: 768px) {
  .footer_circle {
    width: 98%;
    max-width: 768px;
  }

  .mid_hd p {
    font-size: 0.75rem;
  }

  .mid_list_text #p1 {
    font-size: 0.875rem;
  }

  .mid_list #t1 p {
    width: 30px;
    height: 30px;
  }

  .picture .picture_p1 {
    width: 100%;
    height: 200px;
  }

  .picture .picture_p3,
  .picture .picture_p4,
  .picture .picture_p5,
  .picture .picture_p6,
  .picture .picture_p7 {
    width: 48%;
    height: 150px;
  }

  .ft img {
    width: 18px;
    height: 18px;
  }
}

@media screen and (max-width: 480px) {
  .footer_circle {
    width: 98%;
    max-width: 480px;
  }

  .mid_hd p {
    font-size: 0.625rem;
  }

  .mid_list #t1 p {
    width: 28px;
    height: 28px;
  }

  .picture .picture_p1 {
    width: 100%;
    height: 160px;
  }

  .picture .picture_p3,
  .picture .picture_p4,
  .picture .picture_p5,
  .picture .picture_p6,
  .picture .picture_p7 {
    width: 100%;
    height: 120px;
  }

  .ft img {
    width: 16px;
    height: 16px;
  }

  .add img {
    width: 40px;
    height: 40px;
  }

  .add p {
    width: 45px;
    height: 45px;
  }
}
</style>